#include "mypushbutton.h"
#include <QDebug>
#include <QPropertyAnimation>

//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
//{

//}

MyPushButton::MyPushButton(QString normalImg, QString pressImg)
{
    normalImgPath = normalImg;
    pressImgPath  = pressImg;

    QPixmap pix;
    bool ret = pix.load(normalImg);
    if(!ret) {
        qDebug()<< "Loading Pix is fault!";
        return;
    }

    // 设置图片相关属性
    this->setFixedSize(pix.width(), pix.height());
    // 设置不规则样式图片
    this->setStyleSheet("QPushButton{border:0px;}");

    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(), pix.height()));
}

void MyPushButton::zoom1()
{
    // 创建对象
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    // 设置动画时间间隔
    animation->setDuration(2000);
    // 开始位置
    animation->setStartValue(QRect(this->x(), this->y(), width(), height()));
    // 结束位置
    animation->setEndValue(QRect(this->x(), this->y()+10, width(), height()));
    // 设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutInQuad);
    // 开始
    animation->start();

}



void MyPushButton::zoom2()
{
    // 创建对象
    QPropertyAnimation *animation = new QPropertyAnimation(this, "geometry");
    // 设置动画时间间隔
    animation->setDuration(200);
    // 开始位置
    animation->setStartValue(QRect(this->x(), this->y()+10, this->width(), this->height()));
    // 结束位置
    animation->setEndValue(QRect(this->x(), this->y(), this->width(), this->height()));
    // 设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutInQuad);
    // 执行动画
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *e)
{
    if(this->pressImgPath != "") {  // 传入的按下照片不为空，说明需要有按下状态，切换图片
        QPixmap pix;
        bool ret = pix.load(this->pressImgPath);
        if(!ret) {
            qDebug()<< "Loading Pix is fault!";
            return;
        }

        this->setFixedSize(pix.width(), pix.height());
        this->setStyleSheet("QPushButton:{border: 0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));
    }

    // 返回父类，继续进行事件的分发
    return QPushButton::mousePressEvent(e);
}

void MyPushButton::mouseReleaseEvent(QMouseEvent *e)
{
    if(this->pressImgPath != "") {  // 传入的按下照片不为空，说明需要有按下状态，切换回原来的图片
        QPixmap pix;
        bool ret = pix.load(this->normalImgPath);
        if(!ret) {
            qDebug()<< "Loading Pix is fault!";
            return;
        }

        this->setFixedSize(pix.width(), pix.height());
        this->setStyleSheet("QPushButton:{border: 0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));
    }

    // 返回父类，继续进行事件的分发
    return QPushButton::mouseReleaseEvent(e);
}
